public class SortReview {
    //直接插入排序

    public void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0 ; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }



    //希尔排序
    public void shellSort (int[] array) {
        int gap = array.length;
        while((gap /= 2) >= 1) {
            shell(array,gap);
        }
    }
    private void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0 ; j -= gap) {
                if(array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }


    //选择排序1
    public void selectSort1(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if(array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }

    private void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    //选择排序2

    public void selectSort2(int[] array) {
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            int maxIndex = left;
            int minIndex = left;
            for (int i = left+1; i <= right; i++) {
                if(array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if(array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array,left,minIndex);
            //防止第一个元素为 最大值
            //如果第一个元素是最大值，那么上条语句交换后，最大值就会被交换到 minIndex 下标位置
            //所以 此时 maxIndex 就需要维护 到 minIndex 下标位置
            if(maxIndex == left) {
                maxIndex = minIndex;
            }

            swap(array,right,maxIndex);
            left++;
            right--;
        }
    }

    //堆排序

    public void heapSort(int[] array) {
        createHeap(array);//大根堆
        //交换堆顶元素（第一个） 和 堆底元素（最后一个）
        for (int end = array.length-1; end > 0; end--) {
            swap(array,0,end);
            shiftDown(array,0,end);
        }
    }

    private void createHeap(int[] array) {
        //int parent = (array.length - 1 - 1) / 2;
        for (int parent = (array.length - 1 - 1) / 2;
             parent >= 0 ; parent--) {
            shiftDown(array,parent,array.length);
        }
    }
    private void shiftDown(int[] array,int parent, int len) {
        int child = 2 * parent + 1;
        while(child < len) {
            if(child+1 < len && array[child+1] > array[child]) {
                child++;
            }
            if(array[child] > array[parent]) {
                swap(array,parent,child);
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

    //快排：Hoare法
//    public void hoareQuickSort(int[] array) {
//        quick(array,0,array.length-1);
//    }

    public void quickSort(int[] array,int left,int right) {
        if(left >= right) {
            return;
        }
        int par = partition(array,left,right);
        quickSort(array,left,par-1);
        quickSort(array,par+1,right);
    }


    private int partition(int[] array,int left,int right) {
        int par = left;
        int tmp = array[left];
        while(left < right) {
            while(left < right && array[right] >= tmp) {
                right--;
            }
            while(left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,par,left);
        return left;
    }

    //快排：挖坑法


    public void quickSort2(int[] array,int left,int right) {
        if(left >= right) {
            return;
        }
        int par = partition2(array,left,right);
        quickSort2(array,left,par-1);
        quickSort2(array,par+1,right);
    }

    private int partition2(int[] array,int left,int right) {
        //int par = left;
        int tmp = array[left];
        while(left < right) {
            while(left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while(left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }


    //优化1：三数取中法

    public void quickSort3(int[] array,int left,int right) {
        if(left >= right) {
            return;
        }
        int ret = midTreeNum(array,left,right);
        swap(array,ret,left);
        int par = partition(array,left,right);
        quickSort3(array,left,par-1);
        quickSort3(array,par+1,right);
    }

    private int midTreeNum(int[] array,int left,int right) {
        int mid = (left + right) / 2;
        int ret = 0;
        if(array[left] > array[right]) {
            if(array[mid] > array[left]) {
                return left;
            }else if(array[mid] < array[right]){
                return right;
            }else {
                return mid;
            }
        }else {
            if(array[mid] > array[right]) {
                return right;
            }else if(array[mid] < array[left]){
                return left;
            }else {
                return mid;
            }
        }
    }


    //优化2：当递归到小的子区间时，使用插入排序

    public void quickSort4(int[] array,int left,int right) {
        if(left >= right) {
            return;
        }
        if(right - left + 1 == 6) {
            insertSort2(array,left,right);
            return;
        }
        int ret = midTreeNum(array,left,right);
        swap(array,ret,left);
        int par = partition(array,left,right);
        quickSort4(array,left,par-1);
        quickSort4(array,par+1,right);
    }

    public void insertSort2(int[] array,int left,int right) {
        for (int i = left + 1; i <= right; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= left; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    //前后指针法

    public void quickSort5(int[] array,int left,int right) {
        if(left >= right) {
            return;
        }
        int par = partition(array,left,right);
        quickSort5(array,left,par-1);
        quickSort5(array,par+1,right);
    }

    private int partition3(int[] array, int left, int right) {
        int prev = left ;
        int cur = left+1;
        while (cur <= right) {
            if(array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array,cur,prev);
            }
            cur++;
        }
        swap(array,prev,left);
        return prev;
    }

}
